package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.DeleteException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {

    @Autowired
    private SetMealDishService setMealDishService;

    @Override
    @Transactional
    public void addTanJia(SetmealDto setmealDto) {

        //保存套餐信息到setmeal表中;
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map( item -> {
            item.setSetmealId(setmealDto.getId());
            return item;

        }).collect(Collectors.toList());
        // 将数据保存到setmeal表
        setMealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteShan(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> dishMealLqw = new LambdaQueryWrapper<>();
        dishMealLqw.in(Setmeal::getId, ids);
        dishMealLqw.eq(Setmeal::getStatus, 1);
        int count = this.count(dishMealLqw);
        if (count>0) {
            throw new DeleteException("该套餐正进行售卖 不能删除");
        }

        this.removeByIds(ids);
        setMealDishService.removeByIds(ids);

    }

    /**
     * 根据id查
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setMealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(SetmealDto setmealDto) {
//        根据id更新setmeal表
        this.updateById(setmealDto);
        // 操作套餐对应的菜品数据
        LambdaQueryWrapper<SetmealDish> querywrapper1 = new LambdaQueryWrapper<>();
        querywrapper1.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setMealDishService.remove(querywrapper1);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map( item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //修改信息
        setMealDishService.saveBatch(setmealDishes);
    }
}
